<?php
include_once ('class.mssql.php');

class EditOperation
{
    private $comtec_id_machine;
    private $comtec_id_operation;
    private $operation_type;
    private $comtec_id_responsible;
    private $diagnosis;
    private $changed_parts;
    private $notes;
    private $value_work;
    private $value_parts;
    private $value_others;
    private $value_total;
    private $operation_date_start;
    private $operation_date_end;
    private $date_next_maintenance;
    private $status;
    private $delay;
    private $notes_for_owner;
    
    private $errors;
    private $token;

    public function __construct()
    {
        $this->errors   = array();
        
        $this->comtec_id_operation              = $_POST['comtec_id_operation'];
        $this->comtec_id_machine                = $_POST['comtec_id_machine'];
        
        $this->comtec_id_responsible	        = $_POST['comtec_id_responsible'];
        $this->operation_type 			= $_POST['operation_type'];
        $this->operation_date_start 	        = $_POST['op_year1'].$_POST['op_month1'].$_POST['op_day1']." ".$_POST['hour1'].":".$_POST['minute1'].":00";
        $this->operation_date_end	        = $_POST['op_year2'].$_POST['op_month2'].$_POST['op_day2']." ".$_POST['hour2'].":".$_POST['minute2'].":00";
        
        $this->delay                            = $_POST['delay'];
        
        $this->diagnosis 			= $_POST['diagnosis'];
        if(strlen($this->diagnosis) > 400)
        {
         $this->diagnosis = substr($this->diagnosis,0,400);   
        } 
        $this->changed_parts			= $_POST['changed_parts'];
        if(strlen($this->changed_parts) > 400)
        {
         $this->changed_parts = substr($this->changed_parts,0,400);   
        }
        $this->value_parts			= $_POST['value_parts'];
        $this->value_work			= $_POST['value_work'];
        $this->value_others			= $_POST['value_others'];
        
        $this->value_total			= $this->value_parts + $this->value_work + $this->value_others;
        $this->notes				= $_POST['notes'];
        if(strlen($this->notes) > 400)
        {
         $this->notes = substr($this->notes,0,400);   
        }
        $this->notes_for_owner			= $_POST['notes_for_owner'];
        if(strlen($this->notes_for_owner) > 400)
        {
         $this->notes_for_owner = substr($this->notes_for_owner,0,400);   
        }
        $this->status                         = $_POST['status'];
        $this->token				= $_POST['token'];
        
        if($this->operation_type == 1){
         $this->date_next_maintenance           = $_POST['next_day'].$_POST['next_month'].$_POST['next_year']." 00:00:00";
        }
    }

    public function process()
    {
	$this->edit();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function edit()
    {
        $db = new MSSQL();
        
        $tsql  = "UPDATE operation SET ";      
        $tsql .= "comtec_id_responsible= ? , ";           
        $tsql .= "operation_type= ? , ";        
        $tsql .= "delay= ? , ";
        $tsql .= "diagnosis= ? , ";
        $tsql .= "changed_parts= ? , ";                             
        $tsql .= "value_parts= ? , ";
        $tsql .= "value_work= ? , ";
        $tsql .= "value_others= ? , "; 
        $tsql .= "value_total= ?, ";
        $tsql .= "notes= ?, ";
        $tsql .= "notes_for_owner= ?, ";
        $tsql .= "operation_date_start= ?, ";
        $tsql .= "comtec_id_status= ?, ";
        $tsql .= "operation_date_end= ? ";
	$tsql .= " WHERE comtec_id_operation= ? ";
        
        $params = array( 
                         &$this->comtec_id_responsible,
                         &$this->operation_type,
                         &$this->delay,
                         &$this->diagnosis,
                         &$this->changed_parts,
                         &$this->value_parts,
                         &$this->value_work,
                         &$this->value_others,                                              
                         &$this->value_total,
                         &$this->notes,
                         &$this->notes_for_owner,
                         &$this->operation_date_start,
                         &$this->status,
                         &$this->operation_date_end,
                         &$this->comtec_id_operation
                       );
        
        $stmt = sqlsrv_prepare( $db->getConn(), $tsql, $params);  
       if($stmt)
       {
        if(!sqlsrv_execute($stmt))
        { 
         $this->errors[] = 'Error al editar la maquina';
        }elseif($this->operation_type == 1)
            {           
             $tsql  = "UPDATE machines SET ";
             $tsql .= " date_next_maintenance = ? ";
	     $tsql .= " WHERE comtec_id_machine= ? ";            
             $params = array(
                              &$this->date_next_maintenance,
                              &$this->comtec_id_machine
                             );
             $stmt = sqlsrv_prepare($db->getConn(),$tsql,$params);
             sqlsrv_execute($stmt);
            }
       } 
    
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
